<?php
namespace Home\Controller;
use Think\Controller;
/*
	  初始数据
	  会员拥有权限，抓取微信文章功能,查询下级（三级）
*/
class InitController extends Controller {
	public $uid = 0; 	//用户id
	public $mobile = '';//用户手机
	public $info = array();	//用户信息
	public $opt = '';	//站点设置
	protected function _initialize(){
       	//查询会员详细信息
		$this->info 	= session('home_member_info');
		$this->uid      = session('home_uid');
		$register_id 	= I('register_id',0,'intval');  //推广链接上级id
		
		if(!empty($register_id)){
			//上级id
			session('home_register_id',$register_id);
		}
		
		if(!$this->info && !$this->uid){

	       	//微信授权登录返回微信相关信息
			$fans_wx_info = webchecklogin();

			//会员微信信息的主键ID
			session('wx_info_id',$fans_wx_info['id']);

			//微信表关联的user表ID
			$this->uid = $fans_wx_info['uid'];

			//微信授权登录，uid为空，要进行手机号码登录
			if(empty($this->uid)){
				$url = U('Login/new_user');
				header("location:".$url);
				exit();
			}else{
				//微信授权登录，uid不为空，证明可以直接跳过登录
				D('User')->memberAutoLogin($this->uid);
				$this->info = session('home_member_info');
				$this->uid  = session('home_uid');
			}
		}

       	//支付返回参数，有这参数表明支付完成
		$upgrade = I('get.upgrade'); 
        
		//查询记录(加入与session的level不相等,重置session的数据)
		$info = M('User')->where('id = %d',$this->uid)->find();
		$now_level = session('home_member_info.level');
		
		if($info['level'] != $now_level){
			$up_level = true;
		}else{
			$up_level = false;
		}
		
		if(!empty($upgrade) || !empty($up_level)){
			session('home_member_info.level',$info['level']);
			$this->info = session('home_member_info');
		}
		
		//查询会员的信息中心
		$infomation = M('Infomoation')->where(array('uid'=>$this->uid,'is_read'=>0))->count();
		
		//分享代码
		$jsapi = wx_share();

		//分享地址
		$host = "http://".$_SERVER['HTTP_HOST'];

		$header_share_url = U(ACTION_NAME."?register_id={$this->uid}");
		
		$host .= $header_share_url;
		
		//底部导航
		$bottom_menu = bottom_menu();
		$this->assign(array(
			'member_info'=>$this->info,
			'source'=>C('resource'),
			'header_share_url'=>$host,
			'bottom_menu'=>$bottom_menu,
			'infomation'=>$infomation,
			'uid'=>$this->uid,
			'jsapi'=>$jsapi,
		));
		
		//获取站点配置
		$this->opt = D('Options')->getOpts();

		if(!empty($this->opt['alipay'])){
			$this->opt['alipay'] = unserialize($this->opt['alipay']);
		}
		
		if(!empty($this->opt['wx_pay'])){
			$this->opt['wx_pay'] = unserialize($this->opt['wx_pay']);
		}

		if(!empty($this->opt['newspicture'])){
			$this->opt['newspicture'] = "http://".$_SERVER['HTTP_HOST'].$this->opt['newspicture'];
		}

		//查询佣金
		$commission = M('CommissionRule')->find();
		$commission['level_1_brokerage'] += 0;
		$commission['level_2_brokerage'] += 0;
		$commission['level_3_brokerage'] += 0;
		
		$this->opt['commission'] = $commission;
		$this->assign('opt',$this->opt);
    }
	
}